<template>
	<view class="content">
		<scroll-view scroll-y class="left-aside">
			<view v-for="item in flist" :key="item.id" class="f-item b-b" :class="{active: item.id === currentId}" @click="tabtap(item)">
				{{item.name}}
			</view>
		</scroll-view>
		<scroll-view scroll-with-animation scroll-y class="right-aside" @scroll="asideScroll" :scroll-top="tabScrollTop">
			
				<view class="carousel-section">
					<!-- 标题栏和状态栏占位符 -->
					<view class="titleNview-placing"></view>
					<!-- 背景色区域 -->
					<view class="titleNview-background" :style="{backgroundColor:titleNViewBackground}"></view>
					<swiper class="carousel" duration="500" interval="5000" autoplay="true" circular @change="swiperChange">
						<swiper-item v-for="(item, index) in carouselList" :key="index"
							class="carousel-item"><!-- @click="navToDetailPage({title: '轮播广告'})" -->
							<image :src="item.url" />
						</swiper-item>
					</swiper>
					<!-- 自定义swiper指示器 -->
					<view class="swiper-dots">
						<text class="num">{{swiperCurrent+1}}</text>
						<text class="sign">/</text>
						<text class="num">{{swiperLength}}</text>
					</view>
				</view>
				<p class="mmzz">————{{mz}}————</p>
			
			<view v-for="item in slist" :key="item.id" class="t-item" @click="navToList(item.id)">
				<image style="background-color: aqua;" :src="item.picUrl" />
				<text class="s-item">{{item.name}}</text>
				<!-- <view class="t-list">
					<view @click="navToList(item.id, titem.id)" v-if="titem.pid === item.id" class="t-item" v-for="titem in tlist" :key="titem.id">
						<image :src="titem.picture"></image>
						<text>{{titem.name}}</text>
					</view>
				</view> -->
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		getAd
	} from '@/config/api/ad.js';
import {getOrder,getOrder2} from '@/config/api/category.js';
	export default {
		data() {
			return {
				sizeCalcState: false,
				tabScrollTop: 0,
				currentId: 1001002,
				carouselList: [],
				swiperLength: 0,
				swiperCurrent: 0,
				flist: [],
				slist: [],
				tu:"https://www.shequlianshang.com/dts/storage/6q83ocmj1he7swn3xw6x.png?v=1.0",
				mz:"女装/男装"
			}
		},
		onLoad(){
			this.loadData();
		},
		methods: {
			async loadData(){
				// let list = await this.$api.json('cateList');
				// list.forEach(item=>{
				// 	if(!item.pid){
				// 		this.flist.push(item);  //pid为父级id, 没有pid或者pid=0是一级分类
				// 	}else if(!item.picture){
				// 		this.slist.push(item); //没有图的是2级分类
				// 	}else{
				// 		this.tlist.push(item); //3级分类
				// 	}
				// }) 
				
				try { // 广告
					const res = await getAd()
					if (res && res.data && typeof res.data === 'object' && 'code' in res.data && res.data.code ===
						200) {
						this.carouselList = res.data.data
						this.carouselList = this.carouselList.filter(item => item.position === 2);
						console.log(JSON.stringify(this.carouselList, null, 2));
						this.swiperLength = this.carouselList.length;
					} else {
						console.error('请求失败:', res?.data?.msg || '未知错误');
					}
				} catch (error) {
					console.error('接口异常:', error)
					this.$message.error('数据加载失败')
				}
				
				try {	// 一级目录
					const res = await getOrder()
					if (res && res.data && typeof res.data === 'object' && 'code' in res.data && res.data.code === 200) {
						this.flist = res.data.data;
						console.log(this.flist);
					} else {
						console.error('请求失败:', res?.data?.msg || '未知错误');
					}
				} catch (error) {
					console.error('接口异常:', error)
					this.$message.error('数据加载失败')
				}
				
				
				try {	// 二级目录
					const Id = this.currentId;
					const res = await getOrder2({id: Id})
					if (res && res.data && typeof res.data === 'object' && 'code' in res.data && res.data.code === 200) {
						this.slist = res.data.data;
					} else {
						console.error('请求失败:', res?.data?.msg || '未知错误');
					}
				} catch (error) {
					console.error('接口异常:', error)
					this.$message.error('数据加载失败')
				}
			},
			//一级分类点击
			async tabtap(item){
				// if(!this.sizeCalcState){
				// 	this.calcSize();
				// }
				
				this.currentId = item.id;
				this.tu=item.pic_url;
				this.mz=item.name;
				console.log(item.id);
				// let index = this.slist.findIndex(sitem=>sitem.pid === item.id);
				// this.tabScrollTop = this.slist[index].top;
				try {	// 二级目录
					const Id = this.currentId;
					const res = await getOrder2({id: Id});
					if (res && res.data && typeof res.data === 'object' && 'code' in res.data && res.data.code === 200) {
						this.slist = res.data.data;
						console.log(this.slist);
					} else {
						console.error('请求失败:', res?.data?.msg || '未知错误');
					}
				} catch (error) {
					console.error('接口异常:', error)
					this.$message.error('数据加载失败')
				}
			},
			//右侧栏滚动
			asideScroll(e){
				if(!this.sizeCalcState){
					this.calcSize();
				}
				let scrollTop = e.detail.scrollTop;
				let tabs = this.slist.filter(item=>item.top <= scrollTop).reverse();
				if(tabs.length > 0){
					this.currentId = tabs[0].pid;
				}
			},
			//计算右侧栏每个tab的高度等信息
			// calcSize(){
			// 	let h = 0;
			// 	this.slist.forEach(item=>{
			// 		let view = uni.createSelectorQuery().select("#main-" + item.id);
			// 		view.fields({
			// 			size: true
			// 		}, data => {
			// 			item.top = h;
			// 			h += data.height;
			// 			item.bottom = h;
			// 		}).exec();
			// 	})
			// 	this.sizeCalcState = true;
			// },
			navToList(sid){
				uni.navigateTo({
					url: `/pages/product/list?fid=${this.currentId}&sid=${sid}`
				})
			},
			//轮播图切换修改背景色
			swiperChange(e) {
				const index = e.detail.current;
				this.swiperCurrent = index;
				this.titleNViewBackground = this.carouselList[index].background;
			},
		}
	}
</script>

<style lang='scss'>
	page,
	.ttpp{
		
	}
	.mmzz{
		text-align: center;
		color: grey;
		font-size: 28upx;
	}
	.content {
		height: 100%;
		background-color: #fff;
	}

	.content {
		display: flex;
	}
	.left-aside {
		flex-shrink: 0;
		width: 200upx;
		height: 100%;
		background-color: #fff;
	}
	.f-item {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100upx;
		font-size: 28upx;
		color: $font-color-base;
		position: relative;
		&.active{
			color: $base-color;
			background: #fff;
			&:before{
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				height: 36upx;
				width: 8upx;
				background-color: $base-color;
				border-radius: 0 4px 4px 0;
				opacity: .8;
			}
		}
	}

	.right-aside{
		flex: 1;
		overflow: hidden;
		padding-left: 20upx;
		background-color: #fff;
	}
	.s-item{
		display: flex;
		align-items: center;
		height: 70upx;
		padding-top: 8upx;
		font-size: 28upx;
		color: $font-color-dark;
	}
	.t-list{
		display: flex;
		flex-wrap: wrap;
		width: 100%;
		background: #fff;
		padding-top: 12upx;
		&:after{
			content: '';
			flex: 99;
			height: 0;
		}
	}
	.t-item{
		padding: 10px;
		flex-shrink: 0;
		display: inline-block;
		justify-content: center;
		align-items: center;
		flex-direction: column;
		width: 176upx;
		font-size: 26upx;
		color: #fff;
		padding-bottom: 20upx;
		height: 130px;
		image{
			width: 140upx;
			height: 140upx;
		}
	}
	
	/*轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 10px;
	
		.titleNview-placing {
			height: var(--status-bar-height);
			padding-top: 44px;
			box-sizing: content-box;
		}
	
		.titleNview-background {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 300upx;
			transition: .4s;
		}
		
		 .carousel {
		    width: 96%;
		    height: 260upx; /* 轮播图容器高度，与背景高度一致 */
		    
		    .carousel-item {
		      width: 100%;
		      height: 100%; /* 确保子项填充容器 */
		      
		      image {
		        width: 100%;
		        height: 100%; /* 图片撑满容器 */
		        object-fit: cover; /* 保持比例并覆盖容器，避免拉伸变形 */
		      }
		    }
		  }
	}
	
	page {
		.cate-section {
			position: relative;
			z-index: 5;
			border-radius: 16upx 16upx 0 0;
		}
	
		.carousel-section {
			padding: 0;
	
			.titleNview-placing {
				padding-top: 0;
				height: 0;
			}
	
			.carousel {
				.carousel-item {
					padding: 0;
				}
			}
	
			.swiper-dots {
				left: 45upx;
				bottom: 40upx;
			}
		}
	}
	
	.swiper-dots {
		display: flex;
		position: absolute;
		left: 60upx;
		bottom: 15upx;
		width: 72upx;
		height: 36upx;
		background-image: url();
		background-size: 100% 100%;
	
		.num {
			width: 36upx;
			height: 36upx;
			border-radius: 50px;
			font-size: 24upx;
			color: #fff;
			text-align: center;
			line-height: 36upx;
		}
	
		.sign {
			position: absolute;
			top: 0;
			left: 50%;
			line-height: 36upx;
			font-size: 12upx;
			color: #fff;
			transform: translateX(-50%);
		}
	}
	
</style>
